//
//  DiscussTopCell.m
//  OperationPro
//
//  Created by Mac on 16/10/9.
//  Copyright © 2016年 展鸿科技. All rights reserved.
//

#import "DiscussTopCell.h"
#import "DiscussModel.h"

@interface DiscussTopCell()
@property (nonatomic, strong) UILabel *lblName;
@property (nonatomic, strong) UIImageView *headImg;
@property (nonatomic, strong) UILabel *lblDate;
@property (nonatomic, strong) UILabel *lblType;
@property (nonatomic, strong) UILabel *lblCount;
@property (nonatomic, strong) UIImageView *chatView;

@end

@implementation DiscussTopCell

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (void)initView{
    [super initView];
    _headImg=[[UIImageView alloc]init];
    [self addSubview:_headImg];
    [_headImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).mas_offset(20);
        make.top.equalTo(self).mas_offset(15);
        make.size.mas_equalTo(CGSizeMake(50, 50));
    }];
    _headImg.layer.masksToBounds=YES;
    _headImg.layer.cornerRadius=25;
    _headImg.image=[UIImage imageNamed:@"Ico_home_head"];
    _lblName = [[UILabel alloc] init];
    [self addSubview:_lblName];
    [_lblName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.headImg.mas_right).mas_offset(10);
        make.top.equalTo(self.headImg);
    }];
    [_lblName setFont:F(12)];
    [_lblName setTextColor:LightFontColor];
    
    _lblDate = [[UILabel alloc] init];
    [self addSubview:_lblDate];
    [_lblDate mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).mas_offset(-15);
        make.bottom.equalTo(_lblName);
    }];
    [_lblDate setTextColor:LightFontColor];
    [_lblDate setFont:F(12)];
    
    _lblType = [[UILabel alloc] init];
    [self addSubview:_lblType];
    [_lblType mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_lblName);
        make.bottom.equalTo(self).mas_offset(-15);
    }];
    [_lblType setTextColor:MainColor];
    [_lblType setFont:F(14)];
    [_lblType setText:@"置顶"];
    _lblCount = [[UILabel alloc] init];
    [self addSubview:_lblCount];
    [_lblCount mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_lblDate);
        make.bottom.equalTo(_lblType);
    }];
    [_lblCount setTextColor:MainColor];
    [_lblCount setFont:_lblDate.font];
    
    UIImage *imgChat = [UIImage imageNamed:@"Ico_discuss_num"];
    UIImageView *chatView = [[UIImageView alloc] init];
    [self addSubview:chatView];
    [chatView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_lblCount);
        make.size.mas_equalTo(imgChat.size);
        make.right.equalTo(self.lblCount.mas_left).mas_offset(-3);
    }];
    [chatView setImage:imgChat];
    _chatView = chatView;
    
    [self addSubview:self.lblTitle];
    [self.lblTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).mas_offset(60);
        make.bottom.equalTo(_lblType);
        make.right.equalTo(chatView.mas_left).mas_offset(-3);
    }];
    [self.lblTitle setFont:F(14)];
    self.lblTitle.textAlignment=NSTextAlignmentLeft;
    [self.lblTitle setTextColor:FontColor];
   
    UIView *lineView = [[UIView alloc] init];
    [self addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
       make.right.equalTo(self);
        make.left.equalTo(self);
        make.bottom.and.right.equalTo(self);
        make.height.equalTo(@.5);
    }];
    [lineView setBackgroundColor:LineColor];
    
  
}

- (void)setModel:(DiscussModel *)model{
    NSString*urlString =[CClass getCacheData:qnDomain];
    if (model.D_headImg==nil||[model.D_headImg isEqualToString:@""]) {
        _headImg.image=[UIImage imageNamed:@"Ico_home_head"];
        
    }else
    {
        NSURL *headUrl =[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",urlString,model.D_headImg]];
        [_headImg sd_setImageWithURL:headUrl placeholderImage:[UIImage imageNamed:@"Ico_home_head"]];
        
    }
    _lblName.text = model.D_author;
    NSString  *a = model.D_created;
    NSString *timeString =[a substringWithRange:NSMakeRange(0, 16)];
    _lblDate.text = timeString;
    [_lblType setText:@"置顶"];

    NSDictionary *kind = @{@"01":@"提问", @"02":@"讨论", @"03":@"经验"};
    self.lblTitle.text = [NSString stringWithFormat:@"[%@]%@", kind[model.D_kind], model.D_title];
    _lblCount.text = model.D_reply_count;
//    BOOL isnice = [model.D_flag isEqualToString:@"01"];
//    if (isnice) {
//        _lblType.text = @" 精华 ";
//    }else{
//        _lblType.text = @"";
//    }
    CGSize kindsize = [_lblType.text sizeWithFont:_lblType.font constrainedToSize:CGSizeMake(100, WindowHeight)];
    [self.lblTitle mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).mas_offset(80 + kindsize.width+5);
    }];
    
    CGSize size = [_lblCount.text sizeWithFont:_lblCount.font constrainedToSize:CGSizeMake(100, WindowHeight)];
    [_chatView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).mas_offset(-30 - size.width);
    }];

}

+ (float)getCellHeightByModel:(id)model{
    return 80;
}

@end
